package com.ruoyi.school.problem.controller;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.problem.domain.Problem;
import com.ruoyi.school.problem.service.IProblemService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 题目Controller
 * 
 * @author qiang
 * @date 2022-07-10
 */
@Controller
@RequestMapping("/school/problem")
public class ProblemController extends BaseController
{
    private String prefix = "school/problem";

    @Autowired
    private IProblemService problemService;

    @RequiresPermissions("school:problem:view")
    @GetMapping()
    public String problem()
    {
        return prefix + "/problem";
    }

    /**
     * 查询题目列表
     */
    @RequiresPermissions("school:problem:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Problem problem)
    {
        startPage();
        List<Problem> list = problemService.selectProblemList(problem);
        return getDataTable(list);
    }

    /**
     * 导出题目列表
     */
    @RequiresPermissions("school:problem:export")
    @Log(title = "题目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Problem problem)
    {
        List<Problem> list = problemService.selectProblemList(problem);
        ExcelUtil<Problem> util = new ExcelUtil<Problem>(Problem.class);
        return util.exportExcel(list, "题目数据");
    }

    /**
     * 新增题目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add3";
    }

    /**
     * 新增保存题目
     */
    @RequiresPermissions("school:problem:add")
    @Log(title = "题目", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Problem problem)
    {
        return toAjax(problemService.insertProblem(problem));
    }

    /**
     * 修改题目
     */
    @RequiresPermissions("school:problem:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Problem problem = problemService.selectProblemById(id);
        mmap.put("problem", problem);
        return prefix + "/edit";
    }

    /**
     * 修改保存题目
     */
    @RequiresPermissions("school:problem:edit")
    @Log(title = "题目", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Problem problem)
    {
        return toAjax(problemService.updateProblem(problem));
    }

    /**
     * 删除题目
     */
    @RequiresPermissions("school:problem:remove")
    @Log(title = "题目", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(problemService.deleteProblemByIds(ids));
    }
}
